package com.turing.post.datatranferpibs.impl;

import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.datatranferpibs.util.CardManager;
import com.turing.post.datatranferpibs.util.CardManagerDetail;
import com.turing.post.datatranferpibs.util.TradHistoryDetailDao;

@Stateless(mappedName = "datatranferpibs.impl.TradHistoryDetailDaoImpl")
@Remote
@Local
public class TradHistoryDetailDaoImpl implements TradHistoryDetailDao {
	/**
	 * 实体管理器
	 */
	@PersistenceContext
	protected EntityManager _em;

	@SuppressWarnings("unchecked")
	@Override
	public CardManager getCardManagerBySeqNo(String seqNo) {
		String sql = "select * from CardManager where CardSystemSeqNo=?1";
		Query query = _em.createNativeQuery(sql, CardManager.class);
		query.setParameter(1, seqNo);
		List<CardManager> card = query.getResultList();
		if (card == null || card.size() < 0) {
			return null;
		}
		return card.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CardManager> getCardManagerListByTranDate(String branchno,String trandate) {
		String sql = "select * from cardmanager t where t.trandate=to_date(?1,'yyyy-mm-dd') and (?2 is null or branchno=?2)";
		Query query = _em.createNativeQuery(sql, CardManager.class);
		query.setParameter(1, trandate);
		query.setParameter(2, branchno);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getCardManagerBranchNoList(String trandate) {
		String sql = "select t.branchno,count(*) from cardmanager t where t.trandate=to_date(?1,'yyyy-mm-dd') group by branchno order by branchno";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, trandate);
		return query.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CardManagerDetail> getCardManagerDetailList(Integer cardId) {
		String sql = "select * from CardManagerDetail where CardId=?1";
		Query query = _em.createNativeQuery(sql, CardManagerDetail.class);
		query.setParameter(1, cardId);
		List<CardManagerDetail> card = query.getResultList();
		return card;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String[] getTradHistoryDetailByCardManager(CardManager cm) {
		String sql = "select t.canceltranseqno,t.delegaterespcode,t.trancode,t.delegateno from tradhistorydetail t where BranchNo=?1 and TranSeqNo=?2 and TranDate=?3";
		Query query = _em.createNativeQuery(sql);
		query.setParameter(1, cm.getBranchNo());
		query.setParameter(2, cm.getTranSeqNo());
		query.setParameter(3, cm.getTranDate());
		List<Object[]> objs = query.getResultList();
		if (objs == null || objs.size() < 1) {
			return null;
		}
		Object[] obj = objs.get(0);
		return new String[] { getString(obj[0]), getString(obj[1]),
				getString(obj[2]), getString(obj[3]) };

	}

	private String getString(Object obj) {
		if (obj == null) {
			return null;
		} else {
			return obj.toString();
		}
	}
}
